﻿
using DualPlaneFR4Paster.Models;
using Microsoft.EntityFrameworkCore;

namespace DualPlaneFR4Paster
{
    public class DTContext : DbContext
    {
        private string? DbPath;
        public DbSet<XYZRPoint> XYZRPoints { get; set; }
        public DbSet<MParam> MParams { get; set; }
        public DbSet<MProduct> MProducts { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            options.UseLazyLoadingProxies().UseSqlite($"Data Source={DbPath};Pooling=False");
        }
        public DTContext()
        {
            var path = System.AppDomain.CurrentDomain.BaseDirectory;
            DbPath = System.IO.Path.Join(path, "point.db");
        }
        public DTContext(string part)
        {
            var path = System.AppDomain.CurrentDomain.BaseDirectory;
            DbPath = System.IO.Path.Join(path, part, "point.db");
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<XYZRPoint>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired(true).HasMaxLength(32);
                entity.Property(e => e.Alias).IsRequired(false).HasMaxLength(64);
            });
            modelBuilder.Entity<MParam>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired(true).HasMaxLength(32);
                entity.Property(e => e.Type).IsRequired(true).HasMaxLength(16);
                entity.Property(e => e.Alias).IsRequired(true).HasMaxLength(64);
                entity.Property(e => e.Value).IsRequired(true).HasMaxLength(16);
                entity.Property(e => e.Unit).IsRequired(false).HasMaxLength(16);
            });
            modelBuilder.Entity<MProduct>()
                .HasMany(p => p.Pcses)
                .WithOne()
                .OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Seed();
        }
    }
}
